JOBWD=`pwd`

source ${INPUT_1}

OUTPUTSHARED=$shareddir

echo "loading modules on Taito"
module purge
#module load intel
#module load intelmpi
#module load mkl
#module load hypre
#module load mumps
#module load trilinos/11.0.3
#module load elmer/latest
#module load python-env
#module load gmsh
#module load python/2.7.9-gnu492 ## for sisu
module load python-env/intelpython2.7  ## for taito




#ELMERBIN=/usr/local/Elmer_devel_07-19-16/bin/ElmerSolver_mpi
ELMERBIN=/appl/math/elmer/intel-16.0.0/intelmpi-5.1.1/latest/bin/ElmerSolver_mpi
#export LD_LIBRARY_PATH="/usr/local/Elmer_devel_07-19-16/lib/elmersolver"
export LD_LIBRARY_PATH="/appl/math/elmer/intel-16.0.0/intelmpi-5.1.1/latest/lib/elmersolver"




[[ -e $JOBWD/input ]] || ln -sv $STORAGESHARED/input $JOBWD/input
[[ -e $JOBWD/n_list.txt ]] || ln -sv $STORAGESHARED/n_list.txt $JOBWD/n_list.txt


first=`awk 'NR==1 {print $1}' n_list.txt`
# Current row
NR=`sed -n -e "$((COUNTER+1))p" n_list.txt`
nb=`awk '{print $1}' <<< $NR`



[[ -e $JOBWD/Particle_in ]] || ln -sv $OUTPUTSHARED/Particle_in $JOBWD/Particle_in


echo "Calving_$COUNTER : $JOBWD" >> $OUTPUTSHARED/worklfow-jobs.txt


mkdir -p $OUTPUTSHARED/ElmerResults

firstIteration=true
nrbefore=0
nb_before=0

nrafter=`sed -n -e "$((COUNTER+2))p" $JOBWD/input/n_list.bak`
nb_after=`awk '{print $1}' <<< $nrafter`

currentctr=$((COUNTER+1))

echo "COUNTER VARIABLE ::: $COUNTER"
echo "current counter ::: $currentctr"

if [ $currentctr -gt 1 ]; then
  echo "current ctr greater than one"
  nrbefore=`sed -n -e "$((COUNTER))p" $JOBWD/input/n_list.bak`
  nb_before=`awk '{print $1}' <<< $nrbefore`
  firstIteration=false
  echo "previous iteration element ::::  $nb_before"
fi


mkdir -p priorbeta/$nb

mkdir -p Python_particle_n/Date$nb



# Create ElmerResults for nb
mkdir -p ElmerResults/Date$nb
#mkdir -p ElmerResults/Date$nb/

#cd ElmerResults

# Run the particle model

echo "Run the particle model DUMMY .."
cd $JOBWD
mkdir -p $JOBWD/ParticleResults
mkdir -p $JOBWD/ParticleResults/Date$nb
#cd ParticleResults/Date$nb
mkdir -p $JOBWD/Particle_out

# On CSC we use real Particle_Out from Jan's package
[[ -e $OUTPUTSHARED/Particle_out/particle_results$nb.txt ]] || ln -sv $STORAGESHARED/Particle_out/particle_results$nb.txt $JOBWD/Particle_out/particle_results$nb.txt
cp $JOBWD/Particle_out/particle_results$nb.txt  $JOBWD/ParticleResults/Date$nb/maxi
echo "Simulating Dummy Model Calving ... "
# these two lines will be jan*s code

mkdir -p $OUTPUTSHARED/ParticleResults/Date$nb

cp -rv $JOBWD/ParticleResults/Date$nb/maxi $OUTPUTSHARED/ParticleResults/Date$nb/ 

## you need to copy, otherwise python execution will 
## take the shared directory as ref.
## you need to copy, otherwise python execution will 
#cp -rv $STORAGESHARED/input/ClassProperties-Template.py $JOBWD/input/Python_particle/ClassProperties.py
#echo "modifying ClassProperties.py self.path_folder.."
#sed -i -e 's|@@CWD@@|'${JOBWD}'|g' $JOBWD/input/Python_particle/ClassProperties.py


#python $JOBWD/input/Python_particle/ParticleToElmer.py $nb $JOBWD

#echo "copying last iteration's Date folder"
#mv $JOBWD/ElmerResults/Date$nb $OUTPUTSHARED/ElmerResults/
#mkdir -p $OUTPUTSHARED/surf
#cp -rv $JOBWD/surf/$nb_after $OUTPUTSHARED/surf/

cp -rv $JOBWD/surf/$nb_after $OUTPUTSHARED/surf/
  
